package com.datastructure.test.binarytreeandlinkedlist;

import java.util.ArrayList;

public class BinaryAndLinkedList {

    ArrayList<TreeNode> res = new ArrayList<>();
    public static void main(String[] args) {

    }

    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree==null){
            return null;
        }
        midIncur(pRootOfTree);
        for (int i = 0; i < res.size()-1; i++) {
            res.get(i).right=res.get(i+1);
            res.get(i+1).left=res.get(i);
        }
        return res.get(0);
    }

    public void midIncur(TreeNode pRootOfTree){
        if(pRootOfTree.left!=null) {
            midIncur(pRootOfTree.left);
        }
        res.add(pRootOfTree);
        if(pRootOfTree.right!=null) {
            midIncur(pRootOfTree.right);
        }
    }
}

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }
}
